package chances.epg.data.taglib;

import java.util.List;

import javax.servlet.jsp.JspException;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;

import chances.epg.commons.log.EpgLogFactory;
import chances.epg.data.taglib.page.PageInfo;
import chances.epg.entity.content.Episode;
import chances.epg.entity.content.Series;
import chances.epg.navigator.context.AccessContext;
import chances.epg.navigator.context.AccessContextUtils;


/**
 * 查询连续剧剧集.
 * @author Elysium
 *
 */
public class EpisodeTag extends QueryListTag {
	private static final Logger logger = EpgLogFactory.getSystemLogger();

	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = -2524487169879349675L;

	private String code;//Series code
	private int size;
	
	@Override
	public int doEndTag() throws JspException {
		String seriesCode = this.code;
		if(StringUtils.isEmpty(seriesCode)){
			seriesCode = AccessContextUtils.getAccessContext(pageContext).getContentCode();
		}
		
		try{
			if(StringUtils.isNotBlank(this.getPageName())){
				this.queryEpisodeByPage(seriesCode);
			}else if(size > 0){
				this.queryEpisodeBySize(seriesCode);
			}else{
				this.queryEpisode(seriesCode);
			}
		}catch(Throwable ex){
			logger.error("EpisodeTag error.",ex);
		}
		
		return super.doEndTag();
	}
	
	/**
	 * 查询指定数量的条目.
	 */
	private void queryEpisode(String seriesCode) {
		List<Episode> result = this.getContentService().getEpisodes(seriesCode);
		this.setEpisodes(result);
	}
	
	/**
	 * 分页查询.
	 */
	private void queryEpisodeByPage(String seriesCode) {
		PageInfo pageInfo = this.createPageInfo(size);
		Series series = this.getContentService().getSeriesByCode(seriesCode);
		List<Episode> result = this.getContentService().getEpisodes(seriesCode, series.getSeriesType(), pageInfo.getPageBean());
		this.setPageInfo(pageInfo);
		this.setEpisodes(result);
	}
	
	/**
	 * 查询指定数量的条目.
	 */
	private void queryEpisodeBySize(String seriesCode) {
		List<Episode> result = this.getContentService().getEpisodes(seriesCode,this.start,this.size);
		this.setEpisodes(result);
	}
	
	/**
	 * 
	 * @param result
	 */
	private void setEpisodes(List<Episode> result) {
		AccessContext context = AccessContextUtils.getAccessContext(this.pageContext);
		for (Episode episode : result) {
			episode.setUrl(navigateUrlFactory.generatePlayUrl(context, episode));
		}
		
		this.setCollectionResult(result);
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

    /* (non-Javadoc)
     * @see chances.epg.data.taglib.QueryListTag#createPageInfo(int)
     */
    @Override
    protected PageInfo createPageInfo(int pageSize) {
        PageInfo pageInfo = super.createPageInfo(pageSize);
        String pageIndexParamName = this.pageName + "_idx";
        String pageVal = getHttpServletRequest().getParameter(
                pageIndexParamName);
        if (StringUtils.isNotBlank(pageVal)) {
            pageInfo.getPageBean().setCurrentPage(Integer.parseInt(pageVal));
        }
        return pageInfo;
    }
}
